/*请求url*/
var urlData = require('../../../../../data/url.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    itemArray:[],
    checked: false,
    stu_id:[],
    courseId: 0,
    /*url*/
    checkListUrl: urlData.urlList.organManage.organCheck.listOnLoad.url,
    submitUrl: urlData.urlList.organManage.organCheck.submit.url
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    wx.getStorage({
      key: 'organCheckListCourseId',
      success:function(res){
        that.setData({
          courseId: res.data.course_Id
        }); 
        that.sendCourseId(e);
      }
    })
    
  },
  /*
    发送课程数据
  */
  sendCourseId:function(e){
    var that = this;
    wx.request({
      url:this.data.checkListUrl, 
      method: "get",
      data: {
        //发给后台
        course_id:this.data.courseId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.listData(res);
        }
      },
      fail: function (res) {
        console.log('刷list数据失败');
      }
    });
  },
  /*刷新数据*/
  listData: function (res) {
    this.setData({
      itemArray: res.data.list
    });
  },

  /* 多选列表*/
  checkboxChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      stu_id: e.detail.value
    }); 
  }, 

  /*提交按钮*/
  submit: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定以上学生缺勤？',
      success: function (res) {
        if (res.confirm) {
          that.ok(e);
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    })
  },
  ok: function (e) {
    var that = this,
        stuArray = this.data.stu_id,
        i,
        str='';

    for(i=0; i<stuArray.length; i++){
      str = str + stuArray[i] +',';
    }

    wx.request({
      url:this.data.submitUrl,
      method: "get",
      data: {
        stu_id: str,
        course_id:this.data.courseId
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        that.checkPage(e);
      },
      fail: function (res) {
        console.log('选择出勤失败');
      }
    });
  },
  checkPage: function (e) {
    wx.navigateTo({
      url: '../check-page/check-page'
    })
  }
})